package com.example.ui.leftmenu;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.service.MainService;
import com.example.ui.contact.AtMeActivity;
import com.example.ui.contact.CommentMeActivity;
import com.example.ui.contact.MsgState;
import com.example.ui.contact.NoticeActivity;
import com.example.ui.contact.PersonHomeActivity;
import com.example.ui.contact.PraiseMeActivity;
import com.example.ui.contact.adapter.FdListViewAdapter;
import com.example.ui.contact.adapter.MsgListViewAdapter;
import com.example.ui.contact.adapter.MyListViewAdapter;
import com.example.ui.contact.bean.ChatMsgItem;
import com.example.ui.contact.bean.MeItem;
import com.example.ui.contact.bean.NoticeItem;
import com.example.ui.contact.bean.RecentItem;
import com.example.ui.contact.bean.RequestItem;
import com.example.ui.contact.chat.ChatActivity;
import com.example.ui.contact.chat.L;
import com.example.ui.contact.chat.PushMessageReceiver;
import com.example.ui.contact.chat.PushMessageReceiver.EventHandler;
import com.example.ui.contact.db.ChatMsgDB;
import com.example.ui.contact.db.RecentMsgDB;
import com.example.ui.contact.db.RequestMsgDB;

import com.example.util.Util;

public class Item07View extends BaseView {
	//与我相关总数量
	private int msgNum1 = PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS;
	private int msgNum2 =PushMessageReceiver.MSG_FRIEND;//好友请求总数量
	private int msgNum3 =PushMessageReceiver.MSG_CHAT;//私信消息总数量
	
	
	
	
	//Leone 
	private Context context;
	
	public TabHost tabHost;
	public TextView meNum,friendNum,chatNum;
	private Button titleLeftBtn,titleRightBtn;
	public  TextView titleText;
	public  static ListView my_listview;
	public  static ListView fd_listview;
	public  static ListView msg_listview;
	
//	public  static MsgList my_listview;
//	public  static MsgList fd_listview;
//	public  static MsgList msg_listview;

	private String[] title = new String[]{"与我相关","好友请求","私信消息"};
	
	private String[] MyText = {"系统通知","提到我的","评论","赞"};
	private String[] FdText = {"路人甲","路人乙","路人丙","路人丁","路人戊",
			"路人己","路人庚","路人辛"};
	private String[] MsgText = {"朋友甲","朋友乙","朋友丙","朋友丁","朋友戊",
			"朋友己","朋友庚","朋友辛"};
	


	private int[] MsgimgArr = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
			R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
			R.drawable.ic_launcher};	
	

	private View userTab,articeTab,feedTab;  
	private View[] tabs = new View[]{userTab,articeTab,feedTab};  
	private int[] tabIds = new int[]{R.id.tab1,R.id.tab2,R.id.tab3}; 
	    
	public RadioGroup circleGroup;
	
	public static MyListViewAdapter my_adapter;	//与我相关适配器
	private FdListViewAdapter fd_adapter;       //好友请求适配器
	public  MsgListViewAdapter msg_adapter;     //私信消息适配器
	private List<RequestItem> fd_msgList = new ArrayList<RequestItem>();// 好友请求消息对象数组
	private List<RecentItem> recent_msgList = new ArrayList<RecentItem>();// 私信消息列表对象数组
	private List<ChatMsgItem> chat_msgList = new ArrayList<ChatMsgItem>();// 消息对象数组
	private ChatMsgDB chatMsgDB;  //消息数据库
	private RequestMsgDB requestMsgDB;
	private RecentMsgDB recentMsgDB;
	HSApplication hsApplication;
	public Item07View(Context context, HSApplication hsApplication){
		this.context = context;
		this.hsApplication=hsApplication;
	}
	
	public void init(){
		view = LayoutInflater.from(context).inflate(R.layout.item07, null);
		titleLeftBtn = (Button)view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button) view.findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)view.findViewById(R.id.layout_title_middle_text);
		titleRightBtn.setVisibility(View.INVISIBLE);
		
		circleGroup=(RadioGroup)view.findViewById(R.id.circle_tab_radio);
		meNum = (TextView)view.findViewById(R.id.item07_me_num);
		friendNum = (TextView)view.findViewById(R.id.item07_friend_num);
		chatNum = (TextView)view.findViewById(R.id.item07_chat_num);
		
		titleText.setText("与我相关");
		titleLeftBtn.setText("菜单");
		titleLeftBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myMoveView.showHideLeftMenu();
				if(MainService.isLeftMenu==true){
//					my_listview.setEnabled(false);
//					fd_listview.setEnabled(false);
//					msg_listview.setEnabled(false);
					circleGroup.setEnabled(false);
					circleGroup.setOnCheckedChangeListener(null);
				}else{
//					my_listview.setEnabled(true);
//					fd_listview.setEnabled(true);
//					msg_listview.setEnabled(true);
					circleGroup.setEnabled(true);
					circleGroup.setOnCheckedChangeListener(radioGroupClick);
				}
			}
		});
		tabHost=(TabHost)view.findViewById(R.id.item07_tabhost);
		tabHost.setup();
		for(int i=0;i<tabs.length;i++){
            tabs[i] = (View) LayoutInflater.from(context).inflate(R.layout.item07_tabwidget, null);//null);
            TextView text = (TextView) tabs[i].findViewById(R.id.tabwidget_text);

            text.setText(title[i]);
            tabHost.addTab(tabHost.newTabSpec(title[i]).setIndicator(tabs[i]).setContent(tabIds[i])); 
        } 
		//tabHost.setCurrentTab(1);//默认显示第二个“好友请求”
		
		if((PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME
				+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS)==0){//与我相关
			meNum.setVisibility(View.GONE);
		}else{
			meNum.setText(""+(PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME
					+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS));
		}
		
		if(PushMessageReceiver.MSG_FRIEND==0){//好友请求
			friendNum.setVisibility(View.GONE);
		}else{
			friendNum.setText(""+PushMessageReceiver.MSG_FRIEND);
		}
		
		if(PushMessageReceiver.MSG_CHAT==0){//私信消息
			chatNum.setVisibility(View.GONE);
		}else{
			chatNum.setText(""+PushMessageReceiver.MSG_CHAT);
		}
		
		
		
		circleGroup.setOnCheckedChangeListener(radioGroupClick);
//		my_listview = new MsgList(context);
//		my_listview.inflate(context, resource, root)
		my_listview = (ListView)view.findViewById(R.id.tab1_list_view);
		my_adapter = new MyListViewAdapter(context,titleText);
		my_listview.setAdapter(my_adapter);
		my_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
                Util.toast(context, "与我相关"+position);
                Intent intent;
                switch(position){
                case 0://系统通知
                	meNum.setText(""+(msgNum1-PushMessageReceiver.MSG_NOTICE));
                	PushMessageReceiver.MSG_NOTICE = 0;
                	msgNum1 = PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS;
                	
                    intent = new Intent(context, NoticeActivity.class);
                    intent.putExtra("text", MyText[position]);
                    context.startActivity(intent);
                    

                	break;
                case 1://提到我的
                	meNum.setText(""+(msgNum1-PushMessageReceiver.MSG_ME));
                	PushMessageReceiver.MSG_ME = 0;
                	msgNum1 = PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS;
                	
                	intent = new Intent(context, AtMeActivity.class);
                    intent.putExtra("text", MyText[position]);
                    context.startActivity(intent);
                    
                	
                	break;
                case 2://评论
                	meNum.setText(""+(msgNum1-PushMessageReceiver.MSG_COMMENT));
                	PushMessageReceiver.MSG_COMMENT = 0;
                	msgNum1 = PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS;
                	
                    intent = new Intent(context, CommentMeActivity.class);
                    intent.putExtra("text", MyText[position]);
                    context.startActivity(intent);
                    

                	
                	break;
                case 3://赞
                	meNum.setText(""+(msgNum1-PushMessageReceiver.MSG_FUNS));
                	PushMessageReceiver.MSG_FUNS = 0;
                	msgNum1 = PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS;
                	
                    intent = new Intent(context, PraiseMeActivity.class);
                    intent.putExtra("text", MyText[position]);
                    context.startActivity(intent);
                    

                	
                	break;
                		
                }


			}
		});
		fd_listview = (ListView)view.findViewById(R.id.tab2_list_view);
		//initRequestMsgData();
		

		msg_listview = (ListView)view.findViewById(R.id.tab3_list_view);
		//initRencentMsgData();
		msg_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				
				RecentItem item=recent_msgList.get(position);
				recent_msgList.remove(position);
				recentMsgDB.delRecent(item.getUserId());
				msg_adapter.notifyDataSetChanged();
				Intent intent = new Intent(context, ChatActivity.class);
				intent.putExtra("mName", item.getName());
				intent.putExtra("mUserId", item.getUserId());
				context.startActivity(intent);
			}
		});	
		
		
	}
	
	OnCheckedChangeListener radioGroupClick = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_button0:// 与我相关
				tabHost.setCurrentTab(0);
				titleText.setText("与我相关");
				meNum.setText(""+(PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME
						+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS));
				titleRightBtn.setVisibility(View.INVISIBLE);
				break;
			case R.id.radio_button1:// 好友请求
				MsgState.state = MsgState.REQUEST_MSG_STATE;
				initRequestMsgData();
				tabHost.setCurrentTab(1);
				titleText.setText("好友请求");
				friendNum.setText(""+PushMessageReceiver.MSG_FRIEND);
				titleRightBtn.setVisibility(View.INVISIBLE);
				
				break;
			case R.id.radio_button2:// 私信消息
				MsgState.state = MsgState.RECENT_MSG_STATE;
				initRencentMsgData();
				tabHost.setCurrentTab(2);
				titleText.setText("私信消息");
				chatNum.setText(""+PushMessageReceiver.MSG_CHAT);
				titleRightBtn.setVisibility(View.INVISIBLE);

				break;
			default:
					break;
			}
		}
	};
	
	/**
	 * 从数据库中读出私信消息数据
	 */
	public void initRencentMsgData() {
		recentMsgDB = HSApplication.getInstance().getRecentMsgDB();
		List<RecentItem> list = recentMsgDB.getRecentList();  
		recent_msgList.clear();
		if (list.size() > 0) {
			for (RecentItem entity : list) {
				recent_msgList.add(entity);
			}
		}
		msg_adapter = new MsgListViewAdapter(context,titleText,recent_msgList);
		msg_listview.setAdapter(msg_adapter);
		msg_adapter.notifyDataSetChanged();

	}
	
	

	
	/**
	 * 从数据库中读出好友请求消息数据
	 */
	public void initRequestMsgData() {
		requestMsgDB = HSApplication.getInstance().getRequestMsgDB();
		List<RequestItem> list = requestMsgDB.getRequestMsgList();  
		fd_msgList.clear();
		if (list.size() > 0) {
			for (RequestItem entity : list) {

				fd_msgList.add(entity);
			}
		}
		
//		for(int i=0;i<3;i++){
//			RequestItem entity = new RequestItem();
//			fd_msgList.add(entity);
//		}
		fd_adapter = new FdListViewAdapter(context,titleText,fd_msgList);
		fd_listview.setAdapter(fd_adapter);
//		fd_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				L.Debug("onItemClick", String.valueOf(position));
//				L.Debug("onItemClick view.getId()", String.valueOf(view.getId()));
//				
//				switch(view.getId()){
//				case R.id.yesId:
//					L.Debug("onItemClick yesId", String.valueOf(position));
//					Util.toast(context, "holder right onclick"+position);
//					fd_msgList.get(position).setError_type(View.INVISIBLE);
//					fd_msgList.get(position).setRight_type(View.INVISIBLE);
//					fd_adapter.notifyDataSetChanged();
//					break;
//				case R.id.noId:
//					L.Debug("onItemClick noId", String.valueOf(position));
//					Util.toast(context, "holder error onclick"+position);
//					fd_msgList.get(position).setError_type(View.INVISIBLE);
//					fd_msgList.get(position).setRight_type(View.INVISIBLE);
//					fd_adapter.notifyDataSetChanged();
//					break;
//				}
//				
//			}
//			
//		});

		fd_adapter.notifyDataSetChanged();

	}
	




	
	
}
